<?php namespace Summer\Location\Models;

use Illuminate\Support\Facades\DB;
use Model;
use Summer\Location\Behaviors\LocationModel;
use Winter\Storm\Database\Traits\Validation;

/**
 * Model
 */
class UserAddressModel extends Model
{
    use Validation;

    public $implement = [LocationModel::class];

    /**
     * @var string The database table used by the model.
     */
    public $table = 'summer_location_user_address';

    /**
     * @var array Validation rules
     */
    public $rules = [
    ];
    public $belongsTo = [
        'target' => ['RainLab\User\Models\User','key'=>'user_id','otherKey'=>'id'],
    ];
    public function beforeSave()
    {
        if ($this->is_default==1) {
            DB::table($this->table)->where('id', '<>', $this->id)->where('user_id',$this->user_id)->update(['is_default' => false]);
        }
    }
}
